package com.jixiantubu.jinnang;

import java.util.ArrayList;
import java.util.List;
import com.example.jixiantubu.R;
import com.jixiantubu.adapter.JinNang_YouJi_JuTiNeiRong_TuPianAdapter;
import com.jixiantubu.adapter.MyPagerAdapter_youji;
import com.jixiantubu.bean.Photo_library;
import com.jixiantubu.bean.Travelsbean;
import com.jixiantubu.tupianlunbo.ZoomViewPager_fx;
import com.jixiantubu.util.CustomProgressDialog;
import com.jixiantubu.util.MyApplication;
import com.jixiantubu.util.PostGridview;
import com.jixiantubu.util.ReboundScrollView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Point;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class JinNang_YouJi_JuTiNeiRong extends Activity implements
		OnClickListener {
	TextView t_title, t_context;
	ImageView backiImageView, selectiImageView;
	JinNang_YouJi_JuTiNeiRong_TuPianAdapter adapter;
	Travelsbean travelsbean;
	List<Photo_library> list = new ArrayList<Photo_library>();
	ReboundScrollView scrollView;
	PostGridview gridView;
	private int width;
	boolean flag = true;// 判断是否收藏
	 String url="http://" + MyApplication.getIP()
			+ ":8080/Jinnang/KePuTuiJian_Middle";
	HttpUtils httpUtils;
	private List<ZoomViewPager_fx> ziv_view;
	ViewPager viewPager;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Window window = this.getWindow();
		window.requestFeature(window.FEATURE_NO_TITLE);
		setContentView(R.layout.jinnang_youji_jutineirong_activity);
		initViews();
		initData();
	}

	private void initViews() {
		Intent intent = getIntent();
		travelsbean = (Travelsbean) intent.getSerializableExtra("neirong");
		Log.e("!!!!!!!------!!!!!!", travelsbean.toString());
		getwebcheckshoucang(url);
		list = travelsbean.getPl_path();
		t_title = (TextView) findViewById(R.id.jinnang_youji_jutineirong_t_title);
		t_context = (TextView) findViewById(R.id.jinnang_youji_jutineirong_t_content);
		gridView = (PostGridview) findViewById(R.id.jinnang_youji_jutineirong_gridview);
		// 修复了程序进入时GridView会滑动到顶端的小bug。
		scrollView = (ReboundScrollView) findViewById(R.id.jinnang_youji_jutineirong_scrollview);
		scrollView.requestChildFocus(gridView, null);
		backiImageView = (ImageView) findViewById(R.id.JinNang_youji_jutineirong_back);
		selectiImageView=(ImageView) findViewById(R.id.jinnang_youji_jutineirong_shoucang);
		backiImageView.setOnClickListener(this);
		selectiImageView.setOnClickListener(this);
	}

	private void initData() {
		t_context.setText(travelsbean.getT_content());
		t_title.setText(travelsbean.getT_tittle());
		// 获取手机分辨率，这类写法只能放在activity中，其他地方搜索另外的方法
		Display display = getWindowManager().getDefaultDisplay();
		Point size = new Point();
		display.getSize(size);
		width = size.x;
		adapter = new JinNang_YouJi_JuTiNeiRong_TuPianAdapter(
				JinNang_YouJi_JuTiNeiRong.this, list, gridView, width,null);
		gridView.setAdapter(adapter);
		gridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				final AlertDialog dialog = new AlertDialog.Builder(JinNang_YouJi_JuTiNeiRong.this, R.style.Transparent).create();
				View vp = LayoutInflater.from(dialog.getContext()).inflate(R.layout.view_pager_fx, null);
				viewPager = (ViewPager) vp.findViewById(R.id.zoomViewPager);
				MyPagerAdapter_youji	mp = new MyPagerAdapter_youji(JinNang_YouJi_JuTiNeiRong.this,list);
				viewPager.setAdapter(mp);
				viewPager.setCurrentItem(arg2);
				dialog.show();
				dialog.setContentView(vp);
				
			}
		});	
		adapter.notifyDataSetChanged();

	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.JinNang_youji_jutineirong_back:
			finish();
			break;
		case R.id.jinnang_youji_jutineirong_shoucang:
			if (flag) {
				selectiImageView.setImageResource(R.drawable.shoucang);
				 getwebshoucang(url);
				flag = false;
			} else {
				selectiImageView.setImageResource(R.drawable.quxiaoshoucang);
				 getwebquxiaoshoucang(url);
				flag = true;
			}
			break;
		default:
			break;
		}

	}

	private void getwebcheckshoucang(String url) {
		final CustomProgressDialog pd = new CustomProgressDialog(
				JinNang_YouJi_JuTiNeiRong.this, "正在拼命加载数据,请稍等！", R.anim.frame);
		pd.show();
		httpUtils = new HttpUtils();
		RequestParams params = new RequestParams();
		params.addBodyParameter("key", "6");
		params.addBodyParameter("user_id", MyApplication.user_id);
		String t_id = travelsbean.getT_id() + "";
		params.addBodyParameter("t_id", t_id.trim());
		params.addBodyParameter("choice", "3");
		MyApplication.httpclient.configSoTimeout(5000);
		httpUtils.send(HttpMethod.POST, url, params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						MyApplication.show(JinNang_YouJi_JuTiNeiRong.this, "加载数据失败，请检查网络!");
						pd.dismiss();

					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						pd.dismiss();
						String result = arg0.result;
						MyApplication.show(JinNang_YouJi_JuTiNeiRong.this,
								result);
						if (result.equals("已收藏")) {
							flag = false;
							selectiImageView
									.setImageResource(R.drawable.shoucang);
						}else {
							flag = true;
							selectiImageView
									.setImageResource(R.drawable.quxiaoshoucang);
						}

					}
				});
	}

	private void getwebquxiaoshoucang(String url) {
		httpUtils = new HttpUtils();
		RequestParams params = new RequestParams();
		params.addBodyParameter("key", "8");
		params.addBodyParameter("user_id", MyApplication.user_id);
		String t_id = String.valueOf(travelsbean.getT_id());
		params.addBodyParameter("t_id", t_id.trim());
		params.addBodyParameter("choice", "2");
		httpUtils.send(HttpMethod.POST, url, params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						Toast.makeText(JinNang_YouJi_JuTiNeiRong.this,
								"对不起 ，连接服务器失败", 0).show();

					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
/*
						String result = arg0.result;
						MyApplication.show(JinNang_YouJi_JuTiNeiRong.this,
								result);
*/
					}
				});
	}

	private void getwebshoucang(String url) {
		httpUtils = new HttpUtils();
		RequestParams params = new RequestParams();
		params.addBodyParameter("key", "7");
		params.addBodyParameter("user_id", MyApplication.user_id);
		String t_id = String.valueOf(travelsbean.getT_id());
		params.addBodyParameter("t_id", t_id.trim());
		params.addBodyParameter("choice", "1");
		httpUtils.send(HttpMethod.POST, url, params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						Toast.makeText(JinNang_YouJi_JuTiNeiRong.this,
								"对不起 ，连接服务器失败", 0).show();

					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
					/*	String result = arg0.result;
						MyApplication.show(JinNang_YouJi_JuTiNeiRong.this,
								result);*/

					}
				});

	}

}
